// pages/mine/grow/growrecord/growrecord.js
let { AJAX,imgUrl} = require("../../../../utils/util.js")
const App = getApp()
var interval = null //倒计时函数
Page({

   /**
    * 页面的初始数据
    */
   data: { 
      phoneVal: '',
      time: '获取验证码', //倒计时 
      currentTime: 60, //限制60s
      isClick: false, //获取验证码按钮，默认允许点击    
      codeVal: '',
      check_key: "",
      share_member_id: '',
      info: '',
      userInfo: '', 
      is_disabled:true  
   },
 
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
      this.setData({
         navH: App.globalData.navHeight
      })
      if (!wx.getStorageSync("ACCESSTOKEN")) {
         console.log('111')
         App.userInfoReadyCallback = res => {
            wx.setStorageSync('ACCESSTOKEN', res);
            console.log(wx.getStorageSync("ACCESSTOKEN"))
            AJAX("member/auth").then(res => {
               let isAuth = res.data.auth
               if (!isAuth) {
                  return wx.navigateTo({
                     url: '/pages/authorization/authorization',
                  })

               } else {
                  let vm = this
                  if (options.member_id) {
                     let share_member_id = options.member_id
                     this.setData({
                        share_member_id: share_member_id,
                     })
                  }

                  if (options.scene) {
                     let scene = decodeURIComponent(options.scene);
                     console.log(scene)
                     let share_member_id = scene.split('&')[0];
                     console.log(share_member_id)
                     this.setData({
                        share_member_id: scene,
                     })

                  }
                  this.getUserInfo()
                  this.getShaerMemberId()
               }
            })
         }
         return
      }


      AJAX("member/auth").then(res => {
         let isAuth = res.data.auth
         if (!isAuth) {
            return wx.navigateTo({
               url: '/pages/authorization/authorization',
            })
          
         } else {
            let vm = this
            if (options.member_id) {
               let share_member_id = options.member_id
               this.setData({
                  share_member_id: share_member_id,
               })
            }

            if (options.scene) {
               let scene = decodeURIComponent(options.scene);
               console.log(scene)
               let share_member_id = scene.split('&')[0];
               console.log(share_member_id)
               this.setData({
                  share_member_id: scene,
               })

            }
            this.getUserInfo()
            this.getShaerMemberId()
         }  
      })
       

   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function() {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function() {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function() {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function() {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function() {

   },


   
   getShaerMemberId() {
      AJAX("member/member-info", "GET", {
         member_id: this.data.share_member_id
      }).then(res => {
        // console.log(res.data)
         this.setData({
            userInfo: res.data
         })
      })
   },
   // 获取用户信息/member/member-info
   getUserInfo() {
      AJAX("member/member-info").then(res => {
         this.setData({
            info: res.data
         })
      })
   },
   /**
    * 绑定手机号
    */
   // 验证手机号码
   mobileInput(e) {
      this.setData({
         phoneVal: e.detail.value,
      })
     // console.log(this.data.phoneVal)

   },
   // 获取输入验证码
   codeInput(e) {
      
      this.setData({
         codeVal: e.detail.value,
      })
      if (e.detail.value.length == 6){
         this.setData({
            is_disabled:false
         })
      } else{
         this.setData({
            is_disabled: true
         })
      }
      //console.log(this.data.codeVal)
   },
   // 获取验证码
   getCode() {
      let vm = this
      let phone = vm.data.phoneVal
      let ref = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
      if (phone.length == 0) {
         wx.showToast({
            title: '电话号码不能为空',
            icon: 'none',
            duration: 2000
         })
         return
      }
      if (phone.length < 11) {
         wx.showToast({
            title: '手机号码长度有误',
            icon: 'none',
            duration: 2000
         })
         return
      }
      if (!ref.test(phone)) {
         wx.showToast({
            title: '您输入的手机号码格式错误',
            icon: 'none',
            duration: 2000
         })
         return
      }
      // 验证手机号是否被注册过，
      AJAX("member/check-mobile", "POST", {
         mobile: phone
      }).then(res => {     
         
         if (res.status == "fail") {  
             
            wx.showToast({
               title: '您的手机号已被注册',
               icon: 'none',
               duration: 2000
            })
         } else {
            // 先禁止获取验证码按钮的点击
            vm.setData({
               isClick: true,
            })
            // 60s倒计时 setInterval功能用于循环，常常用于播放动画，或者时间显示
            var currentTime = vm.data.currentTime;
            interval = setInterval(function() {
               currentTime--; //减
               vm.setData({
                  time: currentTime + '重新秒后获取'
               })
               if (currentTime <= 0) {
                  clearInterval(interval)
                  vm.setData({
                     time: '获取验证码',
                     currentTime: 60,
                     isClick: false
                  })
               }
            }, 1000);
            /**
             * sms-send/send
             * 获取验证码 
             */ 
            AJAX("sms-send/send", "POST", {
               mobile: phone,
               tmp_code: 'common_code'
            }).then(res => {
               if (res.status == 'succeed') {
                  wx.showToast({
                     title: '获取成功',
                     duration: 2000
                  })
                  this.setData({
                     check_key: res.data.check_key
                  })
               } else {
                  wx.showToast({
                     title: '获取失败',
                     icon: 'none',
                     duration: 2000
                  })
               }
            })
         }
      })
   },
   // 检查验证码
   formSubmit(e) {
      console.log(e.detail.value.password);
      let vm = this
      AJAX("member/bind-parent", "POST", {
         share_member_id: vm.data.share_member_id,
         mobile: vm.data.phoneVal,
         code: vm.data.codeVal,
         check_key: vm.data.check_key
      }).then(res => {
         if (res.status == 'succeed') {
            wx.showToast({
               title: '绑定成功',
               duration: 2000
            })
            setTimeout(function() {
               wx.navigateTo({
                  url: '/pages/mine/backstage/backstage',
               })
            }, 2500)
         } else {
            wx.showToast({
               title: '绑定失败',
               icon: 'none',
               duration: 2000 
            })
         }
      })
   },

   navBack() {
      wx.switchTab({
         url: '/pages/index/index',
      })
   }
})